/**
 * Title :	jFamilyTree generator
 * Description: jFamilyTree generator
 * Version:	provider
 * Copyright (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package jFamilyTree.site;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.util.Properties;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import jFamilyTree.Messages;
import jFamilyTree.commons.Persist;
import jFamilyTree.commons.Utils;
import jFamilyTree.dialogs.main.XSiteDialog;

/**
 * Run site dialog
 * 
 * @author Bernard Bou
 */
public class RunSiteDialog extends JDialog
{
	private static final long serialVersionUID = 1L;

	// C O M P O N E N T S

	/**
	 * Home input
	 */
	private JTextField theHomeTextField;

	/**
	 * Command information
	 */
	private JLabel theCommandLabel;

	/**
	 * Properties (input/output)
	 */
	private Properties theProperties;

	// C O N S T R U C T OR

	/**
	 * Constructor
	 * 
	 * @param theseProperties
	 *        settings
	 */
	public RunSiteDialog(Properties theseProperties)
	{
		super();
		theProperties = theseProperties;
		initialize();
	}

	/**
	 * Initialize
	 */
	private void initialize()
	{
		setTitle(Messages.getString("RunSiteDialog.0")); //$NON-NLS-1$
		setResizable(true);

		// text fields
		theHomeTextField = new JTextField(16);

		// images
		Icon thisIcon = new ImageIcon(MakeSiteDialog.class.getResource("images/siterun.png")); //$NON-NLS-1$
		JLabel thisImageLabel = new JLabel();
		thisImageLabel.setIcon(thisIcon);
		thisImageLabel.setVerticalTextPosition(JLabel.TOP);
		thisImageLabel.setHorizontalTextPosition(JLabel.CENTER);
		thisImageLabel.setText(Messages.getString("RunSiteDialog.1")); //$NON-NLS-1$
		thisImageLabel.setAlignmentX(Component.CENTER_ALIGNMENT);

		// labels
		JLabel thisHtmlFileLabel = new JLabel(Messages.getString("RunSiteDialog.2")); //$NON-NLS-1$

		theCommandLabel = new JLabel();
		theCommandLabel.setToolTipText(Messages.getString("RunSiteDialog.6")); //$NON-NLS-1$
		theCommandLabel.setEnabled(false);
		theCommandLabel.setFont(new Font("Dialog", Font.ITALIC, 10)); //$NON-NLS-1$

		// buttons
		JButton thisRunButton = new JButton(Messages.getString("RunSiteDialog.10")); //$NON-NLS-1$
		JButton thisCancelButton = new JButton(Messages.getString("RunSiteDialog.11")); //$NON-NLS-1$
		JButton thisBrowserButton = new JButton(Messages.getString("RunSiteDialog.3")); //$NON-NLS-1$
		JButton thisModeButton = new JButton(Messages.getString("RunSiteDialog.4")); //$NON-NLS-1$

		// panels
		JPanel thisDataPanel = new JPanel();
		thisDataPanel.setLayout(new FlowLayout());
		thisDataPanel.add(thisHtmlFileLabel);
		thisDataPanel.add(theHomeTextField);

		JPanel thisCommandPanel = new JPanel();
		thisCommandPanel.setLayout(new FlowLayout());
		thisCommandPanel.add(theCommandLabel);

		JPanel thisButtonPanel = new JPanel();
		thisButtonPanel.setLayout(new FlowLayout());
		thisButtonPanel.add(thisBrowserButton);
		thisButtonPanel.add(thisModeButton);
		thisButtonPanel.add(thisCancelButton);
		thisButtonPanel.add(thisRunButton);

		// event
		thisRunButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				run();
				setVisible(false);
			}
		});

		thisCancelButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				setVisible(false);
			}
		});

		thisBrowserButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				final String thisMessage = Messages.getString("RunSiteDialog.5"); //$NON-NLS-1$
				final String[] theseLines = thisMessage.split("\n"); //$NON-NLS-1$
				String thisBrowser = JOptionPane.showInputDialog(null, theseLines);
				if (thisBrowser != null && !thisBrowser.isEmpty())
				{
					theProperties.setProperty("browser", thisBrowser); //$NON-NLS-1$
					theCommandLabel.setText(makeCommand());
				}
			}
		});

		thisModeButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				XSiteDialog thisDialog = new XSiteDialog(theProperties);
				thisDialog.setModal(true);
				thisDialog.setVisible(true);
				if (thisDialog.ok)
					theCommandLabel.setText(makeCommand());
			}
		});

		// change handler
		theHomeTextField.getDocument().addDocumentListener(new DocumentListener()
		{
			public void removeUpdate(DocumentEvent e)
			{
				theCommandLabel.setText(makeCommand());
			}

			public void insertUpdate(DocumentEvent e)
			{
				theCommandLabel.setText(makeCommand());
			}

			public void changedUpdate(DocumentEvent e)
			{
			}
		});

		// assemble
		JPanel thisPanel = new JPanel();
		thisPanel.setLayout(new BoxLayout(thisPanel, BoxLayout.Y_AXIS));
		thisPanel.add(Box.createRigidArea(new Dimension(0, 10)));
		thisPanel.add(thisImageLabel);
		thisPanel.add(thisDataPanel);
		thisPanel.add(thisCommandPanel);
		thisPanel.add(thisButtonPanel);
		thisPanel.add(Box.createRigidArea(new Dimension(0, 10)));

		setContentPane(thisPanel);
	}

	// C O M M A N D . H A N D L I N G

	/**
	 * Make command
	 * 
	 * @return command
	 */
	private String makeCommand()
	{
		String thisCommand;
		thisCommand = theProperties.getProperty("browser", ""); //$NON-NLS-1$ //$NON-NLS-2$
		if (thisCommand.isEmpty())
			return null;
		thisCommand += " "; //$NON-NLS-1$

		String thisMode = theProperties.getProperty("mode"); //$NON-NLS-1$
		if ("NET".equals(thisMode)) //$NON-NLS-1$
		{
			String thisServer = theProperties.getProperty("server", ""); //$NON-NLS-1$ //$NON-NLS-2$
			String thisDirectory = theProperties.getProperty("directory", ""); //$NON-NLS-1$ //$NON-NLS-2$
			thisCommand += "http://" + thisServer + "/"; //$NON-NLS-1$ //$NON-NLS-2$
			if (!thisDirectory.isEmpty())
			{
				thisCommand += thisDirectory;
				thisCommand += "/"; //$NON-NLS-1$
			}
			thisCommand += theHomeTextField.getText();
		}
		else
		{
			String thisPath = theProperties.getProperty("path", ""); //$NON-NLS-1$ //$NON-NLS-2$
			if (!thisPath.endsWith(File.separator))
				thisPath += File.separator;
			thisPath += theHomeTextField.getText();
			File thisFolder = new File(thisPath);
			try
			{
				thisPath = thisFolder.toURI().toURL().toString();
			}
			catch (MalformedURLException thisException)
			{
			}
			thisCommand += thisPath;
		}
		return thisCommand;
	}

	/**
	 * Run command
	 */
	private void run()
	{
		String thisCommand = makeCommand();
		if (thisCommand != null && !thisCommand.isEmpty())
		{
			try
			{
				Runtime.getRuntime().exec(thisCommand);
			}
			catch (Exception e)
			{
				System.err.println("Cannot run " + thisCommand); //$NON-NLS-1$
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * @see java.awt.Dialog#setVisible(boolean)
	 */
	public void setVisible(boolean thisFlag)
	{
		if (thisFlag)
		{
			// read properties into components
			theHomeTextField.setText(theProperties.getProperty("page")); //$NON-NLS-1$
			theCommandLabel.setText(makeCommand());

			pack();
			Utils.center(this);
		}
		else
		{
			// update properties from components
			theProperties.setProperty("page", theHomeTextField.getText()); //$NON-NLS-1$
		}

		super.setVisible(thisFlag);
	}

	/**
	 * Main
	 * 
	 * @param theseArgs
	 *        arguments
	 */
	static public void main(String[] theseArgs)
	{
		UIManager.put("swing.boldMetal", false); //$NON-NLS-1$
		Properties theseSettings = Persist.getSettings("jFamilyTree-generator"); //$NON-NLS-1$
		RunSiteDialog thisDialog = new RunSiteDialog(theseSettings);
		thisDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		thisDialog.setModal(true);
		thisDialog.setVisible(true);
		Persist.saveSettings("jFamilyTree-generator", theseSettings); //$NON-NLS-1$
		System.exit(0);
	}
}
